# -*- coding: utf-8 -*-
import json
import time
import random

from selenium import webdriver
from scrapy import Spider, Request,FormRequest
from MultipleSpider.items import BiliItem


class BiliSpider(Spider):
    name = 'bili'
    allowed_domains = ['bilibili.com']

    # 频道列表
    # private String bashUrl = "https://api.bilibili.com/archive_rank/getarchiverankbypartion?type=jsonp&tid=20&pn=" ;// + i
    # //https://www.bilibili.com/video/av11778873/
    # private String detailUrl = "https://www.bilibili.com/video/av";
    # //https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=1&type=1&oid=11253064
    # private String replyUrl = "https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=1&type=1&oid=";

    video_base_url = 'https://www.bilibili.com{pageUrl}'
    video_info_url = 'https://api.bilibili.com/x/web-interface/archive/stat?aid={infoId}'
    video_list_url = 'https://api.bilibili.com/archive_rank/getarchiverankbypartion?type=jsonp&tid={channelType}&pn={pageNum}'
    video_reply_url = 'https://api.bilibili.com/x/v2/reply?jsonp=jsonp&pn=1&type=1&oid={oid}'
    video_play_url = 'https://api.bilibili.com/playurl?aid={infoId}'
    #cookies = {'LIVE_BUVID':'AUTO7615143672850608','sid':'m5n2850b','buvid3':'48140DD1-5DC6-46CB-AE18-BB38BFD2F76331049infoc','UM_distinctid':'1609753003a339-0dfa21823828b5-163c6657-13c680-1609753003b29b','fts':'1514367288','pgv_pvi':'5630942208','finger':'14bc3c4e'}
    cookies = {}

    def __init__(self, name=None, **kwargs):
        Spider. __init__(self, name=None, **kwargs)
        param = '%s%s' % (int(time.time()),random.randint(0,1000))
        baseUrl = '%s?%s' % ('https://www.bilibili.com',param)
        driver = webdriver.PhantomJS()
        driver.get(baseUrl)
        self.cookies = driver.get_cookies()
        driver.close()

    def start_requests(self):

        start_urls = ['https://www.bilibili.com/video/av20905766/']
        yield Request(start_urls[0],self.prase_video_details)

    #获取视频地址
    def prase_video_url(self, response):
        item = response.meta['key']
        results = json.loads(response.text)
        if 'durl' in results.keys():
            item['length'] = results.get('durl')[0].get('length')
            item['size'] = results.get('durl')[0].get('size')
            item['video_url'] = results.get('durl')[0].get('url')
            item['exType']  = results.get('format')
            item['img'] = results.get('img')
            item['cid'] = results.get('cid')
            item['image_urls'] = [item['img']]
            item['file_urls'] = [item['video_url']]
        yield item
        pass

    #获取视频信息
    def prase_video_info(self,response):
        item=response.meta['key']
        results = json.loads(response.text)

        #扩展信息
        if 'data' in results.keys():
            for key in results.get('data'):
                val = str(results.get('data').get(key))
                if key == "aid":
                    val = "av" + val
                    item['pk'] = val
                else:
                    item[key] = val
        pass

    #获取详情页作者信息
    def prase_video_details(self, response):

        item = BiliItem()
        links = response.css('a[href^="/video/av"]::attr(href)').extract()
        for link in links:
            #请求相关页面
            yield Request(self.video_base_url.format(pageUrl=link), self.prase_video_details)

        #请求相关信息
        aid = response.url.replace('https://www.bilibili.com/video/', '')
        aid = aid.replace('/','')

        #生成item
        item['create_time'] = response.css('#viewbox_report time ::text').extract_first()
        item['title'] = response.css('#viewbox_report h1::text').extract_first()
        item['desc'] = response.css('.info.open::text').extract_first()
        item['pk'] = aid
        item['web_url'] = response.url
        try:
            item['mainClass'] = response.css('#viewbox_report span a ::text')[1].extract()
            item['subClass'] = response.css('#viewbox_report span a ::text')[2].extract()
        except:
            item['mainClass'] = '其他'
            item['subClass'] = '其他'
        reqId = aid.replace('av','')
        yield Request(self.video_info_url.format(infoId=reqId),meta={'key':item},callback=self.prase_video_info)
        yield Request(self.video_play_url.format(infoId=reqId),cookies=self.cookies,meta={'key':item},callback=self.prase_video_url)
        pass


# In [90]: response.css('.u-face.fl img::attr(src)').extract()
# Out[90]: ['//i0.hdslb.com/bfs/face/af85cdc68511c51030398e4badca75e93e17e7a6.jpg@68w_68h.jpg']

# In [52]: response.css('a[href^="/video/av"]::attr(href)').extract()
# Out[52]:
# ['/video/av6504662/',
#  '/video/av11483749/',
#  '/video/av12927911/',
#  '/video/av7575937/',
#  '/video/av10925279/',
#  '/video/av6858429/',
#  '/video/av7393612/',
#  '/video/av17415776/',
#  '/video/av1672864/',
#  '/video/av12556016/',
#  '/video/av12192017/',
#  '/video/av9692803/',
#  '/video/av7207606/',
#  '/video/av20536702/',
#  '/video/av14879884/']

# In [50]: response.css('#viewbox_report span a ::text').extract()
# Out[50]: ['主页', '舞蹈', '宅舞']

# In [51]: response.css('#viewbox_report time ::text').extract()
# Out[51]: ['2017-07-02 09:40:40']

#response.css('#viewbox_report h1::text').extract()
#['\n    【漾空】Pizzicato Drops【一周年纪念】\n  ']

# In [70]: response.css('.info span ::text').extract()
# Out[70]: ['一个无聊的人 (群:146794530 微博:@漾空)', '投稿：17', '粉丝：8106', '+ 关注', '已关注', '取消关注']

# In [85]: response.css('.info.open::text').extract()
# Out[85]:
# ['一周年感谢!作为纪念这次带来的是在维多利亚岛上录的舞,风景真的很棒!然后也是第一次挑战这样风格的舞蹈,也没有基础所以有的动作看起来挺尴尬的,不过现在还只是第一年,以后还有很多时间可以让我继续进步!那么第二年也请多多指教!另外开了个聊美食的群现在什么人也没有大家能都来加一下一起饥饿起来[?]！群号是146794530\n\n摄影感谢 @HiddenArtisan\n振付本家 ☆ ',
#  '\n音源本家 ☆ ',
#  '\n使用音源 ☆ ']

# 频道列表
# https://api.bilibili.com/archive_rank/getarchiverankbypartion?type=jsonp&tid=20&pn=3

# 视频详情
# https://api.bilibili.com/x/web-interface/archive/stat?aid=15906633

# 视频地址
#http://api.bilibili.com/playurl?aid=7575937&quality=48&vtype=mp4




